<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\dctrl\items;

use yii\helpers\ArrayHelper;

/**
 * 范围控件
 * @author cleverstone
 * @since ym2.0
 */
class Range extends Dcontrol
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'range',
        'label' => '',
        'min' => 0,
        'max' => 100,
        'step' => 1,
        'default' => 0,
        'required' => false,
        'comment' => '',
        'style' => '',
        'class' => '',
        'attribute' => '',
        'minWidth' => '',
        'valueLabel' => '',
        'valueUnit' => '',
    ];

    /**
     * 值标签, 如:增涨
     * @param string $label
     * @return $this
     */
    public function valueLabel($label)
    {
        $this->_options['valueLabel'] = (string)$label;
        return $this;
    }

    /**
     * 值单位, 如:%
     * @param string $unit
     * @return $this
     */
    public function valueUnit($unit)
    {
        $this->_options['valueUnit'] = (string)$unit;
        return $this;
    }

    /**
     * 单元格最小宽度
     * @param integer|string $minWidth
     * @return $this
     */
    public function minWidth($minWidth)
    {
        if (is_numeric($minWidth)) {
            $minWidth .= 'px';
        }

        $this->_options['minWidth'] = $minWidth;
        return $this;
    }

    /**
     * 控件标签
     * @param string $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * 最小
     * @param int $min
     * @return $this
     */
    public function min($min)
    {
        $this->_options['min'] = (int)abs($min);
        return $this;
    }

    /**
     * 最大
     * @param int $max
     * @return $this
     */
    public function max($max)
    {
        $this->_options['max'] = (int)abs($max);
        return $this;
    }

    /**
     * 步数
     * @param int $step
     * @return $this
     */
    public function step($step)
    {
        $this->_options['step'] = (int)abs($step);
        return $this;
    }

    /**
     * 默认值
     * @param int $default
     * @return $this
     */
    public function defaultValue($default)
    {
        $this->_options['default'] = (int)$default;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * HTML属性
     * @param array|string $attribute
     * @return $this
     */
    public function attribute($attribute)
    {
        $this->formatHtmlAttribute($attribute);
        $this->_options['attribute'] = $attribute;

        return $this;
    }

    /**
     * @return array
     */
    public function _toArray()
    {
        return $this->_options;
    }
}